/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Processing;

import java.util.Hashtable;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

/**
 *
 *
 */
public class SaveManager
{
    private static final String nameDB = "MySaveGameData";
    private static final String nameDBName = "MySaveGameName";
    private static RecordStore rs = null;
    private static RecordStore rsN = null;
    
    public static boolean OpenDB()
    {
        try
        {
            rs = RecordStore.openRecordStore(nameDB, true);
            rsN = RecordStore.openRecordStore(nameDBName, true);
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }

    public static boolean CloseDB()
    {
        try
        {
            rs.closeRecordStore();
            rsN.closeRecordStore();
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }

    public static boolean Write(String name, byte[] dl)
    {
        try
        {
            rs.addRecord(dl, 0, dl.length);
            byte[] temp = name.getBytes();
            rsN.addRecord(temp, 0, temp.length);
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }

    public static byte[] Read(int id)
    {
        byte[] result = null;
        try
        {
            result = rs.getRecord(id);
        }
        catch(Exception ex)
        {
            return null;
        }
        return result;
    }

    public static Hashtable GetSaveList()
    {
        Hashtable result = new Hashtable();

        try
        {
            RecordEnumeration re = rsN.enumerateRecords(null, null, false);
            while(re.hasNextElement())
            {
                int id = re.nextRecordId();
                String temp = new String(rsN.getRecord(id));
                result.put(temp, new Integer(id));
            }
        }
        catch(Exception ex)
        {
            return null;
        }

        return result;
    }

    public static boolean DeleteAll()
    {
        try
        {
            RecordStore.deleteRecordStore(nameDB);
            RecordStore.deleteRecordStore(nameDBName);
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }
}
